package com.example.demo.utils;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author chen huilong
 * @data 2022/9/20
 */
@Data
public class Response {

    /**
     * 返回码
     */
    private Integer code;

    /**
     * 返回消息
     */
    private String message;

    /**
     * 返回数据
     */
    private Map<String, Object> data = new HashMap<String, Object>();

    private Response() {}

    // 结果返回成功
    public static Response success() {
        Response response = new Response();
        response.setCode(ResultCode.SUCCESS);
        response.setMessage("成功");
        return response;
    }

    // 失败静态方法
    public static Response error() {
        Response response = new Response();
        response.setCode(ResultCode.ERROR);
        response.setMessage("失败");
        return response;
    }

    // 设置返回信息
    public Response message(String message){
        this.setMessage(message);
        return this;
    }

    public Response code(Integer code){
        this.setCode(code);
        return this;
    }

    // 添加返回数据
    public Response data(String key, Object value){
        this.data.put(key, value);
        return this;
    }

    public Response data(Map<String, Object> map){
        this.setData(map);
        return this;
    }
}
